<?php
/*
* @Description: 函数
* @Autor: HuiSir<273250950@qq.com>
* @Date: 2024-02-26 14:46:17
 * @LastEditTime: 2024-08-21 22:57:04
*/
// 主题设置
include( 'theme-options.php' );

// 二开接口
include( 'rest-api.php' );

// 全局options变量
$GLOBALS['options'] = get_option('theme_options');
function theme_options( $key ) {
	return $GLOBALS['options'][$key];
};

//恢复友链
add_filter( 'pre_option_link_manager_enabled', '__return_true' );

//背景
add_custom_background();

// 移除顶栏
add_filter( 'show_admin_bar', '__return_false' );

// 缩略图
add_image_size( 'thumbnail', 750, 280, true );

//移除WP无用标签
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_filter( 'the_content', array( $GLOBALS[ 'wp_embed' ], 'autoembed' ), 8 );
remove_action( 'pre_post_update', 'wp_save_post_revision' );
wp_deregister_script( 'autosave' );
remove_filter( 'the_content', 'wptexturize' );
remove_filter( 'comment_text', 'make_clickable',  9 );

//防pingback攻击
add_filter( 'xmlrpc_methods', 'remove_xmlrpc_pingback_ping' );
function remove_xmlrpc_pingback_ping( $methods ) {
    unset( $methods[ 'pingback.ping' ] );
    return $methods;
};

//去谷歌字体
add_filter( 'gettext_with_context', 'wpjam_disable_google_fonts', 888, 4 );
function wpjam_disable_google_fonts( $translations, $text, $context, $domain ) {
    $google_fonts_contexts = array( 'Open Sans font: on or off', 'Lato font: on or off', 'Source Sans Pro font: on or off', 'Bitter font: on or off' );
    if ( $text == 'on' && in_array( $context, $google_fonts_contexts ) ) {
        $translations = 'off';
    }
    return $translations;
};

//图片添加alt属性
add_filter( 'the_content', 'image_alt' );
function image_alt( $c ) {
    global $post;
    $title = $post->post_title;
    $s = array( '/src="(.+?.(jpg|bmp|png|jepg|gif))"/i'=> 'src="$1" alt="'.$title.'"' );
    foreach ( $s as$p => $r ) {
        $c = preg_replace( $p, $r, $c );
    }
    return $c;
};

//取消描述中的p标签
function deletehtml( $description ) {
    $description = trim( $description );
    $description = strip_tags( $description, '' );
    return ( $description );
};
add_filter( 'category_description', 'deletehtml' );

//统计
function custom_the_views( $post_id, $echo = true, $views = '' ) {
    $count_key = 'views';
    $count = get_post_meta( $post_id, $count_key, true );
    if ( $count == '' ) {
        delete_post_meta( $post_id, $count_key );
        add_post_meta( $post_id, $count_key, '0' );
        $count = '0';
    }
    if ( $echo )
        echo number_format_i18n( $count ) . $views;
    else
        return number_format_i18n( $count ) . $views;
};
function set_post_views() {
    global $post;
    $post_id = $post->ID;
    $count_key = 'views';
    $count = get_post_meta( $post_id, $count_key, true );
    if ( is_single() || is_page() ) {
        if ( $count == '' ) {
            delete_post_meta( $post_id, $count_key );
            add_post_meta( $post_id, $count_key, '0' );
        } else {
            update_post_meta( $post_id, $count_key, $count + 1 );
        }
    }
};
add_action( 'get_header', 'set_post_views' );

//编辑器增强
function enable_more_buttons( $buttons ) {
    $buttons[] = 'hr';
    $buttons[] = 'del';
    $buttons[] = 'sub';
    $buttons[] = 'sup';

    $buttons[] = 'fontselect';
    $buttons[] = 'fontsizeselect';
    $buttons[] = 'cleanup';

    $buttons[] = 'styleselect';
    $buttons[] = 'wp_page';
    $buttons[] = 'anchor';
    $buttons[] = 'backcolor';
    return $buttons;
};
add_filter( 'mce_buttons_3', 'enable_more_buttons' );

//自定义菜单
register_nav_menus(
    array(
        'header-menu' => __( '导航自定义菜单' ),
        'foot-menu' => __( '页脚自定义菜单' ),
        'js-menu' => __( '原生js' ),
        'es6-menu' => __( 'ES6+' ),
        'js-jiqiao' => __( 'js语法技巧及易错点' ),
        'qdjc-menu' => __( '前端基础' ),
        'dom-menu' => __( 'DOM' ),
        'bom-menu' => __( 'BOM' ),
        'vue-menu' => __( 'Vue2' ),
        'vue3-menu' => __( 'Vue3' ),
        'react-menu' => __( 'React' ),
        'node-menu' => __( 'Nodejs' ),
        'css-menu' => __( 'CSS' ),
        'sf-menu' => __( '算法' ),
        'kfrz-menu' => __( '开发认知和心得' ),
        'xnyh-menu' => __( '性能优化' ),
        'git-menu' => __( 'Git' ),
        'qt-menu' => __( '其他' )
    )
);

//文章归档页面 | 基于 wphsooooo
function wphs_archives_list() {
    if ( !$output = get_option( 'wphs_db_cache_archives_list' ) ) {
        $output = '<div class="archives-by-month"><h2>按月份归档</h2><p class="collapse"><a href="javascript:void(0)">全部展开 / 收缩</a></p>';
        $args = array(
            'post_type' => array( 'archives', 'post', 'zsay' ),
            'posts_per_page' => -1, //全部 posts
            'ignore_sticky_posts' => 1 //忽略 sticky posts
        );
        $the_query = new WP_Query( $args );
        $posts_rebuild = array();
        $year = $mon = 0;
        while ( $the_query->have_posts() ) : $the_query->the_post();
        $post_year = get_the_time( 'Y' );
        $post_mon = get_the_time( 'm' );
        $post_day = get_the_time( 'd' );
        if ( $year != $post_year ) $year = $post_year;
        if ( $mon != $post_mon ) $mon = $post_mon;
        $posts_rebuild[ $year ][ $mon ][] = '<li>'. get_the_time( 'd 日: ' ) .'<a target="_blank" href="'. get_permalink() .'">'. get_the_title() .'</a> <em>('. get_comments_number( '0', '1', '%' ) .'条评论)</em></li>';
        endwhile;
        wp_reset_postdata();

        foreach ( $posts_rebuild as $key_y => $y ) {
            $y_i = 0;
            $y_output = '';
            foreach ( $y as $key_m => $m ) {
                $posts = '';
                $i = 0;
                foreach ( $m as $p ) {
                    ++$i;
                    ++$y_i;
                    $posts .= $p;
                }
                $y_output .= '<li><span class="title">'. $key_m .' 月 ('. $i .' 篇文章)</span><ul class="archives_post_list">';
                //输出月份
                $y_output .= $posts;
                //输出 posts
                $y_output .= '</ul></li>';
            }
            $output .= '<h3 class="archives_year">'. $key_y .' 年 ('. $y_i .' 篇文章)</h3><ul class="archives_mon_list">';
            //输出年份
            $output .= $y_output;
            $output .= '</ul>';
        }

        $output .= '</div>';
        update_option( 'wphs_db_cache_archives_list', $output );
    }
    echo $output;
};

// 清空 wphs_archives_list
function clear_db_cache_archives_list() {
    update_option( 'wphs_db_cache_archives_list', '' );
}
// 新发表文章/修改文章时
add_action( 'save_post', 'clear_db_cache_archives_list' );
?>